<?php

namespace backend\controllers;

use common\services\NewsCategoryServices;
use Yii;
use common\models\NewsCategory;
use yii\data\ActiveDataProvider;
use backend\base\BaseBackController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;

/**
 * NewsCategoryController implements the CRUD actions for NewsCategory model.
 */
class NewsCategoryController extends BaseBackController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all NewsCategory models.
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('../tree/index');
    }

    public function actionTree(){
        return $this->renderAjax('../tree/tree');
    }

    public function actionList(){
        $treeNodeId = Yii::$app->request->get('treeNodeId');

        $service = new NewsCategoryServices();
        $dataProvider = $service->getList($treeNodeId);

        $model = null;

        if (!empty($treeNodeId) && is_numeric($treeNodeId)){
            $model = NewsCategory::findOne($treeNodeId);
        }

        return $this->renderAjax('list', [
            'dataProvider' => $dataProvider,
            'treeNodeId' => $treeNodeId,
            'model' => $model,
        ]);

    }

    public function actionTreeList(){

        $service = new NewsCategoryServices();
        $data = $service->getData();
        return $service->getTree($data);
        /*return $this->renderAjax('tree', [
            'data' => $data,
        ]);*/
    }

    /**
     * Displays a single NewsCategory model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->renderAjax('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new NewsCategory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $parent_id = Yii::$app->request->get('parent_id');
        $model = new NewsCategory();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            if ($model->save()) {
                return ['result' => 'success', 'message' => ''];
            }else{
                return ['result' => 'other', 'message' => join('', $model->firstErrors)];
            }
        } else {
            $model->parent_id = $parent_id ? intval($parent_id) : 0;
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing NewsCategory model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            if ($model->save()) {
                return ['result' => 'success', 'message' => ''];
            }else{
                return ['result' => 'other', 'message' => join('', $model->firstErrors)];
            }
        } else {
            return $this->renderAjax('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing NewsCategory model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $service = new NewsCategoryServices();
        $child = $service->getChildCount($id);
        if ($child > 0) {
            return ['result' => 'fail', 'message' => '分类下存在子类'];
        } else {
            $this->findModel($id)->delete();
            return ['result' => 'success'];
        }
    }

    /**
     * Finds the NewsCategory model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return NewsCategory the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = NewsCategory::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionStatus($id, $status)
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        if ( Yii::$app->request->isPost) {
            $model = NewsCategory::findOne($id);
            $model->status = $status;
            $model->save();
            return ['result' => 'success'];
        }
        else
        {
            return ['result' => 'failure'];
        }
    }
}
